package com.example.dao;

import com.example.bean.Order;
import com.example.bean.OrderItem;
import com.example.bean.Product;
import com.example.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单
 */
public class OrderDao {

    ResultSet resultSet = null;
    Statement statement = null;
    Connection connection = null;
    PreparedStatement preparedStatement = null;

    public void saveOrder(Connection connection, Order order)  {
        try {
            preparedStatement = connection.prepareStatement("insert into orders values (?, ?, ?, ?, ?, ?, ?, ?)");
            preparedStatement.setString(1, order.getOid());
            preparedStatement.setDate(2, new Date(order.getOrderTime().getTime()));
            preparedStatement.setDouble(3, order.getTotal());
            preparedStatement.setInt(4, order.getState());
            preparedStatement.setString(5, order.getAddress());
            preparedStatement.setString(6, order.getName());
            preparedStatement.setString(7, order.getTelephone());
            preparedStatement.setString(8, order.getUid());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet, statement, null);
        }
    }

    public void saveOrderItem(Connection connection, OrderItem orderItem) {
        try {
            preparedStatement = connection.prepareStatement("insert into orderitem values (?, ?, ?, ?, ?)");
            preparedStatement.setString(1, orderItem.getItemId());
            preparedStatement.setInt(2, orderItem.getCount());
            preparedStatement.setDouble(3, orderItem.getSubtotal());
            preparedStatement.setInt(4, orderItem.getProduct().getId());
            preparedStatement.setString(5, orderItem.getOid());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet, statement, null);
        }
    }

    public Order selectOrderByOid(String oid) {
        Order order = null;
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement("select * from orders where oid = ?");
            preparedStatement.setString(1, oid);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                order = new Order();
                order.setOid(oid);
                order.setOrderTime(resultSet.getDate("ordertime"));
                order.setTotal(resultSet.getDouble("total"));
                order.setState(resultSet.getInt("state"));
                order.setAddress(resultSet.getString("address"));
                order.setName(resultSet.getString("name"));
                order.setTelephone(resultSet.getString("telephone"));
                order.setUid(resultSet.getString("uid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
        return order;
    }

    public int updateOrder(Order order) {
        int n = 0;
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement("update orders set address = ?, name = ?, telephone = ?" +
                    " where oid = ?");
            preparedStatement.setString(1, order.getAddress());
            preparedStatement.setString(2, order.getName());
            preparedStatement.setString(3, order.getTelephone());
            preparedStatement.setString(4, order.getOid());
            n = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
        return n;
    }

    public List<Order> selectOrderByUid(String id, int pageNo, int pageSize) {
        List<Order> orderList = new ArrayList<>();
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement("select * from orders where uid = ? limit ?, ?");
            preparedStatement.setString(1, id);
            preparedStatement.setInt(2, (pageNo - 1) * pageSize);
            preparedStatement.setInt(3, pageSize);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Order order = new Order();
                order.setOid(resultSet.getString("oid"));
                order.setOrderTime(resultSet.getDate("ordertime"));
                order.setTotal(resultSet.getDouble("total"));
                order.setState(resultSet.getInt("state"));
                order.setAddress(resultSet.getString("address"));
                order.setName(resultSet.getString("name"));
                order.setTelephone(resultSet.getString("telephone"));
                order.setUid(resultSet.getString("uid"));
                orderList.add(order);
            }
            for (Order order : orderList) {
                preparedStatement = connection.prepareStatement("select * from orderitem o, product p" +
                        " where o.pid = p.pid and oid = ?");
                preparedStatement.setString(1, order.getOid());
                resultSet = preparedStatement.executeQuery();
                while (resultSet.next()) {
                    OrderItem orderItem = new OrderItem();
                    orderItem.setItemId(resultSet.getString("itemid"));
                    orderItem.setCount(resultSet.getInt("count"));
                    orderItem.setSubtotal(resultSet.getDouble("subtotal"));
                    Product product = new Product();
                    product.setId(resultSet.getInt("pid"));
                    product.setName(resultSet.getString("pname"));
                    product.setShop_price(resultSet.getDouble("shop_price"));
                    product.setImage(resultSet.getString("pimage"));
                    orderItem.setProduct(product);
                    orderItem.setOid(order.getOid());
                    order.getList().add(orderItem);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
        return orderList;
    }

    public int selectOrderCount(String id) {
        int count = 0;
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement("SELECT COUNT(*) FROM orders where uid = ?");
            preparedStatement.setString(1, id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                count = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
        return count;
    }
}